﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Macds.Core.XML
{
    public class DefaultClassBuilder : ITextBuilder
    {
        public string ClassNameFlag { get; set; }
        public string ClassGlobalText { get; set; }
        public string ClassPropertyHolder { get; set; }
        public string PropertyNameFlag { get; set; }
        public string PropertyTypeFlag { get; set; }
        public string PropertyGlobalText { get; set; }

        public DefaultClassBuilder()
        {
            #region Class Information

            ClassNameFlag = "[classname]";
            ClassPropertyHolder = "[properties]";
            ClassGlobalText = "public class " + ClassNameFlag + "\r\n{\r\n " + ClassPropertyHolder + "}";
            ClassGlobalText = "[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]\r\n" + ClassGlobalText;

            #endregion

            #region Property Information

            PropertyNameFlag = "[propertyname]";
            PropertyTypeFlag = "[propertytype]";
            PropertyGlobalText = "\tpublic " + PropertyTypeFlag + " " + PropertyNameFlag + " { get; set; }\r\n";

            #endregion
        }

        public string BuildClassText(ClassSchema classSchema)
        {
            string text = ClassGlobalText.Replace(ClassNameFlag, classSchema.Name);

            if (classSchema.HasProperties)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var prop in classSchema.Properties)
                {
                    string replace = PropertyGlobalText.Replace(PropertyNameFlag, prop.Name);

                    switch (prop.Type)
                    {
                        case PropertyType.String:
                            replace = replace.Replace(PropertyTypeFlag, "string");
                            break;
                        case PropertyType.Custom:
                            replace = replace.Replace(PropertyTypeFlag, prop.CustomClass.Name);
                            break;
                        case PropertyType.List:
                            if (prop.ListType == PropertyType.Custom)
                            {
                                replace = replace.Replace(PropertyTypeFlag, "List<" + prop.CustomClass.Name + ">");
                            }
                            else
                            {
                                replace = replace.Replace(PropertyTypeFlag, "List<string>");
                            }
                            break;
                    }

                    switch (prop.XmlType)
                    {
                        case PropertyXmlType.Attribute:
                            replace = "\t[System.Xml.Serialization.XmlAttributeAttribute()]\r\n" + replace;
                            break;
                        case PropertyXmlType.Element:
                            replace = "\t[System.Xml.Serialization.XmlElementAttribute(\""+ prop.Name + "\")]\r\n" + replace;
                            break;
                        case PropertyXmlType.Text:
                            replace = "\t[System.Xml.Serialization.XmlTextAttribute()]\r\n" + replace;
                            break;
                    }

                    sb.Append(replace);
                }

                text = text.Replace(ClassPropertyHolder, sb.ToString());
            }

            return text;
        }
    }
}
